<template>
  <div id="wid" class="cardList" style="float:right;height: 100%">
      <el-header style=" display: flex; align-items: center">
        <a-upload
          style="margin-left: 50px"
          enctype="multipart/form-data"
          action="http://localhost:3000/file/upload"
          :directory="true"
          :multiple="true"
          @change="handleChange"
        >
          <el-button>
            <a-icon type="upload"/>
            Upload
          </el-button>
        </a-upload>
      </el-header>
        <div style="width: 100%;" class="layout-side-n">
          <a  style="color: #222222" @click="rootPath">根路径</a>
          <div v-if="uploadUrl!='upload'">
            <el-divider direction="vertical"></el-divider>
            <a style="color: #222222" @click="upper">返回上一层</a>
            <el-divider direction="vertical"></el-divider>
            <span>{{uploadUrl}}</span>
          </div>
        </div>
        <el-table
          :data="uploadList"
          style="width: 100%">
          <el-table-column
            class=""
            prop="name"
            label="文件名"
            width="180">
            <template slot-scope="scope">
              <div @click="uploadPathClick(scope.row.name)">
                <svg class="icon" aria-hidden="true" >
                  <use  :xlink:href="[handleJudgeFileType(scope.row.name)===0?'#icon-tupian':
                                                   handleJudgeFileType(scope.row.name)===1?'#icon-yuanwenjian-1':
                                                   handleJudgeFileType(scope.row.name)===2?'#icon-f-zip':
                                                   handleJudgeFileType(scope.row.name)===3?'#icon-shipinwenjian':
                                                   handleJudgeFileType(scope.row.name)===4?'#icon-word':
                                                   handleJudgeFileType(scope.row.name)===5?'#icon-ppt':
                                                   handleJudgeFileType(scope.row.name)===6?'#icon-XLS':
                                                   handleJudgeFileType(scope.row.name)===7?'#icon-mp':
                                                   handleJudgeFileType(scope.row.name)===8?'#icon-txt':'#icon-wenjianjia']"></use>
                </svg>
                <a style="margin-left: 10px">{{ scope.row.name }}</a>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="size"
            label="大小"
            width="180">
          </el-table-column>
          <el-table-column
            v-if="tiemFlng"
            prop="mtime"
            label="修改日期">
          </el-table-column>
        </el-table>
  </div>
</template>

<script>
  import {Button, Container, Header, Main, Icon, Table, TableColumn,Divider } from 'element-ui'
  import {Upload} from 'ant-design-vue'
  // import '../../assets/icon/iconfont.css'
  import {judgeFileType,bitConvert} from '../../lib/fileClassification'
  export default {
    name: "File",
    components: {
      AUpload: Upload,
      ElButton: Button,
      ElContainer: Container,
      ElHeader: Header,
      ElMain: Main,
      ElIcon: Icon,
      ElTableColumn: TableColumn,
      ElTable: Table,
      ElDivider:Divider
    },
    data() {
      return {
        scrollWidthData:0,
        tiemFlng:true,
        qwe: '#icon-wenjianjia',
        uploadUrl: 'upload',
        uploadUrlCloning: 'upload',
        uploadList: [],
        iconList: '',
      }
    },
    mounted() {
      this.uploadPath()
      window.addEventListener("resize",this.onWidth,false)
    },
    methods: {
      onWidth(){
        if(document.getElementById("wid")){
          this.scrollWidthData=document.getElementById("wid").scrollWidth
          if(this.scrollWidthData+270<=768){
            this.tiemFlng=false
          }else {
            this.tiemFlng=true
          }
        }
      },
      //上传文件
      handleChange(file) {
        this.uploadPath()
      },
      //点击页面文件夹时
      uploadPathClick(url){
        let that=this
        if (url.indexOf('.')!=-1) {
          return
        }
        that.uploadUrl = that.uploadUrl + '/' + url
        this.uploadPath(that.uploadUrl)

      },
      //获取当前所有文件与文件夹
      uploadPath(url) {
        let that = this
        that.$axios.post('http://localhost:3000/file/uploadPath', {url: url==undefined?that.uploadUrl:url}).then(function (response) {
          console.log(response);
          that.uploadList = response.data.DirList.concat(response.data.fileList)
          let list=[]
          for (let v of that.uploadList){
            list.push({
              name:v.name,
              size:bitConvert(v.size),
              mtime:v.mtime,
            })
          }
          that.uploadList=list
        }).catch(function (error) {
          console.log(error);
        });
      },
      //获取返回值进行icon图标分类
      handleJudgeFileType(e){
        return judgeFileType(e)
      },
      //第一层
      rootPath(){
        this.uploadPath(this.uploadUrlCloning)
        this.uploadUrl=this.uploadUrlCloning
      },
      //上一层
      upper(){
        const that=this
        for (let i=that.uploadUrl.length;i>=0;i--) {
          if(that.uploadUrl[i]=='/'){
            that.uploadUrl=that.uploadUrl.substr(0,i)
            this.uploadPath(this.uploadUrl)
            break
          }
        }
      }
    }
  }
</script>


<style lang="scss" scoped>
</style>

<style scoped>
  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }


  .image {
    width: 100%;
    display: block;
  }

  .el-col {
    border-radius: 4px;
    margin-bottom: 10px;
  }

  .bg-purple-light {
    background: #e5e9f2;
  }

  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }

  .icon {
    width: 2em;
    height: 2em;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
  }

  .layout-side-n{
    display: flex;
    align-items: center;
    width: 100%;
  }

</style>
